<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise.resolve和Promise.reject</title>
</head>
<body>
    <h1>Promise.resolve()和Promise.reject()</h1>

    <script>
        // Promise.resolve()
        // 参数是一个 Promise 实例
        const p1 = Promise.resolve(Promise.resolve('Hello'));
        p1.then(result => console.log(result)); // Hello

        // 参数是一个thenable对象
        const thenable = {
            then: function(resolve, reject) {
                resolve('thenable');
            }
        };
        Promise.resolve(thenable).then(value => {
            console.log(value); // "thenable"
        });

        // 参数不是具有then方法的对象，或根本就不是对象
        const p2 = Promise.resolve('直接返回');
        p2.then(s => console.log(s)); // 直接返回

        // Promise.reject()
        const p3 = Promise.reject('出错了');
        p3.catch(error => console.log(error)); // 出错了

        // reject的参数会原封不动地作为reject的理由
        const thenable2 = {
            then(resolve, reject) {
                reject('还是出错了');
            }
        };
        Promise.reject(thenable2).catch(e => {
            console.log(e === thenable2); // true
        });
    </script>
</body>
</html> 